package cn.yunhe.services.impl;

import cn.yunhe.dao.ITopicDao;
import cn.yunhe.entity.Topic;
import cn.yunhe.services.ITopicServices;
import lombok.Getter;
import lombok.Setter;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Getter
@Setter
public class TopicServices implements ITopicServices {
    @Resource
    private ITopicDao topicDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Topic> search(Topic topic) {
        return topicDao.search(topic);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Topic> searchByArgs(String hql) {
        return topicDao.searchByArgs(hql);
    }

    @Override
    public Topic get(int tid) {
        return topicDao.get(tid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
    public void add(Topic topic) {
        topicDao.add(topic);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
    public void add(List<Topic> list) {
        try {
            for (Topic t : list) {
                topicDao.add(t);
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
    public void del(int tid) {
        topicDao.del(tid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
    public void up(Topic topic) {
        topicDao.up(topic);
    }
}
